package com.apobates.forum.toro.core;

/**
 * 比较符号枚举
 * 支持: 大于(GT), 大于等于(GE), 小于(LT), 小于等于(LE), 等于(EQ), 不等于(NE)
 */
public enum BoolFunType {
    EQ("=="),
    GE(">="),
    GT(">"),
    NE("!="),
    LT("<"),
    LE("<=");

    private final String operator;

    BoolFunType(String operator) {
        this.operator = operator;
    }

    public String getOperator() {
        return operator;
    }

    /**
     *
     * @param obj1
     * @param obj2
     * @param type
     * @param executor
     * @return
     */
    public boolean evaluate(Object obj1, String obj2, String type, StringExpressionExecutor executor) {
        /*
        Number n1 = (Number)obj1;
        Number n2 = NumberUtils.createNumber(obj2);
        int compare = Double.compare(n1.doubleValue(), n2.doubleValue());
        if(getOperator().equals("==") && compare == 0){
            return true;
        }
        if(getOperator().equals(">=") && (compare == 0 || compare == 1)){
            return true;
        }
        if(getOperator().equals(">") && compare == 1){
            return true;
        }
        if(getOperator().equals("!=") && (compare == -1 || compare == 1)){
            return true;
        }
        if(getOperator().equals("<") && compare == -1){
            return true;
        }
        if(getOperator().equals("<=") && (compare == 0 || compare == -1)){
            return true;
        }
        return false;*/
        String n1 = obj1.toString();
        String expression = String.format("%s %s %s", n1, getOperator(), obj2);
        return executor.execute(expression).map(obj->obj instanceof Boolean?(Boolean)obj:false).orElse(false);
    }

    /**
     *
     * @param names
     * @return
     */
    public static BoolFunType getInstance(String names) {
        BoolFunType ins = null;
        for(BoolFunType ele:values()){
            if(ele.name().toLowerCase().equals(names.toLowerCase())){
                ins = ele;
                break;
            }
        }
        return ins;
    }
}
